<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
</html>
<script>
	//数组常用方法
	//join()连接数组中的每一个元素，返回值为字符串
	var arr = [1,2,3];
	console.log(arr.join("-"));
	console.log(arr.join());//默认用逗号隔开
    //稀疏数组
	arr = [,,,];//逗号后面的不算，所以只有三个
	console.log(arr.join("ABC"));//用ABC隔开

	function repeating(str, n){
		//通过构造函数创建一个长度为n+1的数组
		 var arr = new Array(n + 1);
		 return arr.join(str);
	}
	console.log(repeating("ABC",5));
    
    //数组的长度，数组中元素的个数
	console.log(arr.length);

	//pop()删除数组的最后一个元素，返回删除的元素
	arr = [5,6,7,8,9];
	var r = arr.pop();
	console.log(arr);
	console.log(r);

	//向数组的末尾追加元素，返回数组的长度
	r = arr.push("hello");
	console.log(arr);
	console.log(r);
	r =arr.push("ni","hao","a");
	console.log(arr);
	console.log(r);
     
     //删除数组的首元素，并返回该元素
	r = arr.shift();
	console.log(arr);
	console.log(r);
    
    //向数组的首位添加元素，并返回数组的长度
	r = arr.unshift("你好");
	console.log(r);
	console.log(arr);

	r = arr.unshift("hhhh","xixixi");
	console.log(r);
	console.log(arr);

	//sort()排序方法，默认按照字符串进行升序排列
	arr = [12,1,3,21,2];
	arr.sort();
	console.log(arr);

	//
	arr = [3,21,5,12,1,2];
	function compare(a,b){
		//升序排列
		//return a - b;
		//降序排列
		return b - a;
	}
	arr.sort(compare);
	console.log(arr);

	Array.prototype.wgsort = function(func) {

		for (var i = 0; i < this.length - 1; i++) {
			for (var j = 0; j < this.length - 1 - i; j++) {
				var a = this[j];
				var b = this[j+1];

				if (func(a, b) > 0) {
					var temp = this[j];
					this[j] = this[j+1];
					this[j+1] = temp;
				}
			}
		}

	};

	arr = [3, 2, 6, 7, 4];
	arr.wgsort(function(a, b) {
		return a - b;
	});


	//数组逆序
	arr = [3,2,1,5,8];
	arr.reverse();
	console.log(arr);

	Array.prototype.wgreverse = function(){
		var n = this.length / 2;
		n = Math.floor(n);
		for(var i = 0; i < n; i++){
			var temp = arr[i];
			arr[i] = arr[this.length-1-i];
			arr[this.length-1-i] = temp;
		}
	};

	//拼接

	arr = [1,2,3];
	r = arr.concat(55,66,77,88,[1,2,3]);
	console.log(r);
	

	//slice 截取数组
	arr = [1,2,3,4,5];
	r = arr.slice(2,4);
	console.log(r);
	console.log(arr);

	//splice增删改
	//第一个参数：start；
	//第二个参数：要删除的元素个数
	//后面的参数：要插入的参数
	//返回值;删除的元素返回的数组
	arr = [1,2,3,4];
	r = arr.splice(0,0,0,-1);
	console.log(r);//返回值;删除的元素返回的数组
	console.log(arr);

	r = arr.splice(0,2);
	console.log(r);
	console.log(arr);
    
    //1,2,3,4
	arr.splice(2,0,5,6);
	console.log(r);
	console.log(arr);

	//1,2,5,6,3,4
	arr.splice(2,2,7);
	console.log(arr);

	//indexOf,lastindexOf 参照字符串用法

	Array.prototype= function() {}

	//map()
	r = [1,2,3].map(function(item,index){
		return item = item;

	});

	//filter()过滤方法
	arr = ["我恨你","你恨我","你恨他","他很你"];
	r = arr.filter(function(item,index,a){
		return item.indexOf("我") != -1;
	});
	console.log(r);

	//every()
	//参数为回调，
	//每一个回调都返回true结果才为true
    r = [2,4,6,8,9].every(function(item,index){
    	return item % 2 == 0;
    });
    console.log(r);

    //some()
    //有一个回调返回true结果就为true
    r = [12,3,4,5,6,9,3].some(function(item,index){
    	return item % 3 == 0;
    });
    console.log(r);

    //reduce
    //从左往右迭代
    var = [1,2,3,4,5];
    var sum = arr.reduce(function(pre,cur,index,arr){
    	console.log(pre,cur,index);
    	return pre + cur;

    });

</script>